package com.gwz.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gwz.sys.constast.SYSConstast;
import com.gwz.sys.domain.Role;
import com.gwz.sys.domain.User;
import com.gwz.sys.mapper.RoleMapper;
import com.gwz.sys.mapper.UserMapper;
import com.gwz.sys.service.UserService;
import com.gwz.sys.utils.DataGridView;
import com.gwz.sys.utils.MD5Utils;
import com.gwz.sys.vo.UserVo;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RoleMapper roleMapper;

	@Override
	public User queryUserByLoginName(String loginName) {
		return userMapper.queryUserByLoginName(loginName);
	}

	@Override
	public DataGridView queryAllUser(UserVo userVo) {
		Page<Object> page = PageHelper.startPage(userVo.getPage(), userVo.getLimit());
		List<User> data = userMapper.queryAllUser(userVo);
		return new DataGridView(page.getTotal(), data);
	}

	@Override
	public int queryMaxOrderNumber() {
		return userMapper.queryMaxOrderNumber();
	}

	@Override
	public List<User> queryUserByDeptId(UserVo userVo) {
		return userMapper.queryAllUser(userVo);
	}

	@Override
	public void addUser(UserVo userVo) {
		userMapper.insertSelective(userVo);
	}

	@Override
	public User queryUserById(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public void deleteUser(UserVo userVo) {
		userMapper.deleteByPrimaryKey(userVo.getId());
	}

	@Override
	public void resetUserpwd(UserVo userVo) {
		userVo.setSalt(MD5Utils.createSalt());
		userVo.setPwd(MD5Utils.md5(SYSConstast.USER_PWD_DEFAULT, userVo.getSalt(), 2));
		userMapper.updateByPrimaryKeySelective(userVo);
	}

	@Override
	public DataGridView queryUserRole(UserVo userVo) {
		Role role = new Role();
		role.setAvailable(SYSConstast.SYS_AVAILABLE_TRUE);
		// 查询所有角色
		List<Role> allRole = this.roleMapper.queryAllRole(role);
		// 根据用户ID查询拥有的角色
		List<Role> userRole = this.roleMapper.queryRoleByUserId(userVo.getId());
		List<Map<String, Object>> data = new ArrayList<>();
		for (Role r1 : allRole) {
			Boolean LAY_CHECKED = false;
			for (Role r2 : userRole) {
				if (r1.getId() == r2.getId()) {
					LAY_CHECKED = true;
					break;
				}
			}
			Map<String, Object> map = new HashMap<>();
			map.put("id", r1.getId());
			map.put("name", r1.getName());
			map.put("remark", r1.getRemark());
			map.put("LAY_CHECKED", LAY_CHECKED);
			data.add(map);
		}
		return new DataGridView((long)data.size(), data);
	}

	@Override
	public void saveUserRole(UserVo userVo) {
		Integer uid = userVo.getId();
		Integer[] ids = userVo.getIds();
		userMapper.deleteUserRoleByUserId(uid);
		for (Integer rid : ids) {
			userMapper.saveUserRole(rid,uid);
		}
	}

}
